package com.fitns.user.service.baseConfig;

import com.fitns.user.dao.baseConfig.WarnConfigDao;
import com.fitns.user.exception.ServiceException;
import com.fitns.user.exception.WarnConfigMsgCode;
import com.fitns.user.model.baseConfig.TbWarnConfig;
import com.fitns.user.utils.RandomCodeUtils;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.lang.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class WarnConfigService {

	@Autowired
	private WarnConfigDao warnConfigDao;

    /**
     * 新增
     * @param tbWarnConfig
     * @return
     */
	public TbWarnConfig insert(TbWarnConfig tbWarnConfig){
        if(Validator.isNull(tbWarnConfig)){
			throw ServiceException.create(WarnConfigMsgCode.WARN_CONFIG_DATA_IS_NULL);
		}
        String warnNumber =  "YJ"+RandomCodeUtils.getRandomCode(9);
		tbWarnConfig.setWarnNumber(warnNumber);
        String nowDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Timestamp createDate=Timestamp.valueOf(nowDate);
		tbWarnConfig.setCreateTime(createDate);
        tbWarnConfig.setModifyTime(createDate);
		warnConfigDao.insert(tbWarnConfig);
		return tbWarnConfig;
	}

    /**
     * 修改
     * @param tbWarnConfig
     * @return
     */
    public TbWarnConfig modify(TbWarnConfig tbWarnConfig) {
        if(Validator.isNull(tbWarnConfig.getId())){
            throw ServiceException.create(WarnConfigMsgCode.WARN_CONFIG_ID_IS_NULL);
        }
        String nowDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Timestamp modifyDate=Timestamp.valueOf(nowDate);
        tbWarnConfig.setModifyTime(modifyDate);
        warnConfigDao.modify(tbWarnConfig);
        return tbWarnConfig;
    }

    /**
     * 查询列表
     * @param tbWarnConfig
     * @return
     */
    public PageInfo<TbWarnConfig> selectList(TbWarnConfig tbWarnConfig) {
        List<TbWarnConfig> warnConfigList = warnConfigDao.selectList(tbWarnConfig);
        PageInfo<TbWarnConfig> pageInfo=new PageInfo<>(warnConfigList);
        return pageInfo;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public long delete(Long id){
        if(Validator.isNull(id)){
            throw ServiceException.create(WarnConfigMsgCode.WARN_CONFIG_ID_IS_NULL);
        }
        warnConfigDao.delete(id);
        return id;
    }

    /**
     * 通过id从查询记录
     * @param id
     * @return
     */
    public TbWarnConfig selectById(Long id){
        return warnConfigDao.selectById(id);
    }

}
